package com.wolves.career.controller;


import com.wolves.career.entity.RealTimeForumComment;
import com.wolves.career.entity.RealTimeForumThumbsUp;
import com.wolves.career.entity.RealTimeForumTopic;
import com.wolves.career.service.RealTimeForumCommentService;
import com.wolves.career.service.RealTimeForumThumbsUpService;
import com.wolves.career.service.RealTimeForumTopicService;
import com.wolves.career.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 麦翔盛
 * @since 2021-10-27
 */
@RestController
@RequestMapping("/forum")
@CrossOrigin
public class RealTimeForumThumbsUpController {

    @Autowired
    private RealTimeForumThumbsUpService thumbsUpService;
    @Autowired
    private RealTimeForumTopicService topicService;
    @Autowired
    private RealTimeForumCommentService commentService;


    /**
     * 点赞
     * @param thumbsUp
     * @return
     */
    public R doLike(@RequestBody RealTimeForumThumbsUp thumbsUp){
        Integer type = thumbsUp.getType();
        String commentTopicId = thumbsUp.getCommentTopicId();
        //保存点赞信息
        thumbsUpService.save(thumbsUp);
        if (type == 1){
            //如果是文章就给该文章点赞数 + 1
            RealTimeForumTopic topic = topicService.getById(commentTopicId);
            Integer count = topic.getThumbsUp();
            count++;
            topic.setThumbsUp(count);
            topicService.updateById(topic);
        }else {
            RealTimeForumComment comment = commentService.getById(commentTopicId);
            Integer count = comment.getThumbUpNumber();
            count++;
            comment.setThumbUpNumber(count);
            commentService.updateById(comment);
        }
        return R.ok().data("success",true);
    }

    /**
     * 取消点赞
     * @param thumbsUp
     * @return
     */
    @DeleteMapping("/deleteLike")
    public R removeLike(@RequestBody RealTimeForumThumbsUp thumbsUp){
        Integer type = thumbsUp.getType();
        String commentTopicId = thumbsUp.getCommentTopicId();
        String thumbsUpId = thumbsUp.getThumbsUpId();
        //删除点赞信息
        thumbsUpService.removeById(thumbsUpId);
        if (type == 1){
            //如果是文章就给该文章点赞数 - 1
            RealTimeForumTopic topic = topicService.getById(commentTopicId);
            Integer count = topic.getThumbsUp();
            count--;
            topic.setThumbsUp(count);
            topicService.updateById(topic);
        }else {
            //给评论点赞 - 1
            RealTimeForumComment comment = commentService.getById(commentTopicId);
            Integer count = comment.getThumbUpNumber();
            count--;
            comment.setThumbUpNumber(count);
            commentService.updateById(comment);
        }
        return R.ok().data("success",true);
    }

}

